package com.good.akkaserver.server;

import org.apache.commons.pool.impl.StackObjectPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.good.akkaserver.event.ClientEvent;
import com.good.akkaserver.event.ClientEventFactory;
import com.good.akkaserver.event.ObjectPoolManage;

/**
 * 单例模式 保存一些对象池
 * @author GengQing
 * 2014-4-2
 */
public class ObjectPoolWorld  {

	private static Logger logger = LoggerFactory.getLogger(ObjectPoolWorld.class);
	// 对象池定义
	private static StackObjectPool clientEventPool = ObjectPoolManage.getPool(
			ClientEventFactory.class, 10000);

	
	
	private static ObjectPoolWorld objectPoolWorld = null;// 实例
	
	public synchronized static ObjectPoolWorld getInstance() {
		if (objectPoolWorld == null) {
			objectPoolWorld = new ObjectPoolWorld();
		}
		return objectPoolWorld;
	}
	

	private ObjectPoolWorld() {
		logger.info("***************GameWorld 实例化***************");
	}


	/**
	 * 通过对象池申请一个服务器事件对象
	 * @return：服务器事件对象
	 */
	public ClientEvent getClientEvent() {
		synchronized (clientEventPool) {
			try {
				ClientEvent event = (ClientEvent) clientEventPool
						.borrowObject();
				return event;
			} catch (Exception e) {
				logger.error("获取ServerEvent对象出错");
				return new ClientEvent();
			}
		}
	}



	/**
	 * 添加 Function name:returnServerEvent Description: 把服务器事件对象返回给对象池
	 * 
	 * @param event：服务器事件对象
	 */
	public void returnClientEvent(ClientEvent event) {
		synchronized (clientEventPool) {
			try {
				event.reset();
				clientEventPool.returnObject(event);
			} catch (Exception e) {
				logger.error("返回ServerEvent到对象池出错");
			}
		}
	}

 


	
}
